//
//  BIUpdateVersionView.m
//  BIProjectModule
//
//  Created by yown on 2022/2/11.
//

#import "BIUpdateVersionView.h"
#import "BIProjectContext.h"

#import <Masonry/Masonry.h>

@interface BIUpdateVersionView ()

@property (nonatomic) UIImageView *bgImageView;
@property (nonatomic) UITextView *logTextView;
@property (nonatomic) UIButton *closeBtn;
@property (nonatomic) UIButton *upgradeBtn;

@property (nonatomic, assign) BOOL forceUpdate;
@property (nonatomic, copy) NSString *updateInfo;
@property (nonatomic, copy) NSString *downloadLink;

@end

@implementation BIUpdateVersionView

#pragma mark - Life Cycle

+ (void)showWithForceUpdate:(BOOL)forceUpdate
                 updateInfo:(NSString *)updateInfo
               downloadLink:(NSString *)downloadLink {
    UIWindow *keyWindow = [BIProjectContext sharedInstance].appDelegate.window;
    BIUpdateVersionView *view = [[BIUpdateVersionView alloc] initWithFrame:keyWindow.bounds];
    view.forceUpdate = forceUpdate;
    view.updateInfo = updateInfo;
    view.downloadLink = downloadLink;
    [view bi_setupView];
    [view bi_setupLayout];
    [view bi_setupData];
    [keyWindow addSubview:view];
}

#pragma mark - Setup

- (void)bi_setupView {
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    // 背景图
    self.bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"VersionUpgradeBg"]];
    self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:self.bgImageView];
    // 升级提示文本
    self.logTextView = [UITextView new];
    self.logTextView.editable = NO;
    [self.bgImageView addSubview:self.logTextView];
    // 关闭按钮
    self.closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeBtn setImage:[UIImage imageNamed:@"VersionWhiteCloseBtn"] forState:UIControlStateNormal];
    [self.closeBtn addTarget:self action:@selector(bi_closeBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.closeBtn];
    // 立即升级按钮
    self.upgradeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.upgradeBtn addTarget:self action:@selector(bi_upgradeBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImageView addSubview:self.upgradeBtn];
}

- (void)bi_setupLayout {
    // 背景
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
    }];
    // 关闭按钮
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bgImageView);
        make.bottom.mas_equalTo(self.bgImageView.mas_top);
    }];
    // 立即升级按钮
    [self.upgradeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@(60));
        make.left.right.bottom.mas_equalTo(0);
    }];
    // 升级日志文本
    [self.logTextView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.width.mas_equalTo(@(220));
        make.height.mas_equalTo(@(125));
        make.bottom.mas_equalTo(self.upgradeBtn.mas_top).offset(-5);
    }];
}

- (void)bi_setupData {
    self.closeBtn.hidden = self.forceUpdate;
    self.logTextView.text = self.updateInfo;
}

#pragma mark - Action

- (void)bi_closeBtnDidClick {
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)bi_upgradeBtnDidClick {
    NSURL *iTunesURL = [NSURL URLWithString:self.downloadLink];
    if ([[UIApplication sharedApplication] canOpenURL:iTunesURL]) {
        [[UIApplication sharedApplication] openURL:iTunesURL];
    }
}

@end
